//
//  GCMHistoryViewController.m
//  GeeCityManager
//
//  Created by 聚城科技 on 16/11/18.
//  Copyright © 2016年 聚城科技. All rights reserved.
//

#import "GCMHistoryViewController.h"
#import "GCMSheBeiRequst.h"
#import "GCMSBHisModel.h"
#import "GCMSBHistoryTableViewCell.h"
#import "GCMSBPlanModel.h"
#import "GCMSBPlanTableViewCell.h"

@interface GCMHistoryViewController ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation GCMHistoryViewController{

    UITableView * _tableV;
    NSMutableArray *_dataSource;
    NSInteger _currentPage;
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    _currentPage = 1;
    [self createTitle];
    [self createTableView];
    [self downLoadData];

}

-(void)downLoadData{

    self.type = @"Plan";
    [GCMSheBeiRequst postScanDataWithSBId:self.sbid WithURL:SHEBPLAN_URL WithType:@"Plan" Success:^(id obj, NSError *err) {
        NSLog(@"+nihao +1+  %@",obj);
        [_dataSource removeAllObjects];
        _dataSource = obj;
        [_tableV reloadData];
    }];
}
//创建头上面的
-(void)createTitle{
    
    UISegmentedControl * segmented = [[UISegmentedControl alloc] initWithItems:@[@"巡检计划", @"历史记录"]];
    segmented.frame = CGRectMake(0, 0, 150, 30);
    segmented.selectedSegmentIndex = 0; //设置默认选中项
    [segmented addTarget:self action:@selector(segmentedAction:) forControlEvents:UIControlEventValueChanged]; //添加事件
    self.navigationItem.titleView = segmented;
    
}

- (void)segmentedAction:(id)sender {
    if ([sender isKindOfClass:[UISegmentedControl class]]) {
        UISegmentedControl * segment = sender;
        if (segment.selectedSegmentIndex == 0) {
            self.type = @"Plan";
            [GCMSheBeiRequst postScanDataWithSBId:self.sbid WithURL:SHEBPLAN_URL WithType:@"Plan" Success:^(id obj, NSError *err) {
                NSLog(@"+nihao +1+  %@",obj);
                [_dataSource removeAllObjects];
                _dataSource = obj;
                [_tableV reloadData];
            }];
        }else if (segment.selectedSegmentIndex == 1) {
            self.type = @"History";
            [GCMSheBeiRequst postScanDataWithSBId:self.sbid WithURL:SHEBHIST_URL WithType:@"History" Success:^(id obj, NSError *err) {
                [_dataSource removeAllObjects];
                _dataSource = obj;
                [_tableV reloadData];
                 NSLog(@"历史 %@",_dataSource);
            }];
        }
    }
}

-(void)createTableView{
    _tableV = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    _tableV.backgroundColor  = [UIColor redColor];
    _tableV.delegate = self;
    _tableV.dataSource =self;
    _tableV.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1.0];
    [self.view addSubview:_tableV];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //    巡检计划
    GCMSBPlanTableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
    if (!cell1) {
        cell1 = [[GCMSBPlanTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell1"];
    }
    [cell1 setBackgroundColor:[UIColor colorWithRed:.93 green:.93 blue:.93 alpha:1]];
   
    //    历史记录
    GCMSBHistoryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[GCMSBHistoryTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    [cell setBackgroundColor:[UIColor colorWithRed:.93 green:.93 blue:.93 alpha:1]];
    
    if ([self.type isEqualToString:@"Plan"]) {
        GCMSBPlanModel *planModel = _dataSource[indexPath.row];
        cell1.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell1 setPlanModel:planModel];
        return cell1;
        
    }else{
    
        GCMSBHisModel *listModel = _dataSource[indexPath.row];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell setHisModel:listModel];
       return cell;
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSource.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([self.type isEqualToString:@"Plan"]) {
        return 130;
    }else{
        return 160;
    }
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
